package com.atguigu.demo03_knife4j.controller.admin;

import com.atguigu.demo03_knife4j.bean.Emp;
import com.atguigu.demo03_knife4j.result.Response;
import com.atguigu.demo03_knife4j.service.EmpService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/emp")
@Tag(name="员工管理模块") //对Controller模块描述的注解，只在knife4j(swagger3)中生效
public class AdminEmpController {
    @Autowired
    EmpService empService;
    //一般使用统一响应类 封装数据返回
    //  方便前端统一解析返回的结果，也可以通过失败的R对象获取失败的原因
    @Operation(summary = "查询所有员工") //对接口描述的注解
    @GetMapping
    public Response<List<Emp>> list(){
        return Response.ok(empService.list());
    }
    @Operation(summary = "分页查询员工列表")
    @GetMapping("page/{pageNum}/{pageSize}")
    public Response<Page<Emp>> page(@Parameter(name = "pageNum", // 当前注解绑定的形参
                                                description ="页码" , //表示参数的介绍
                                                example="1", // 表示swagger文档中的案例
                                                required = true, //表示参数是否必须，true表示必须
                                                in = ParameterIn.PATH    //表示参数的位置/类型  PATH表示路径参数
                                        )
                                        @PathVariable("pageNum") Long pageNum,
                                    @Parameter(description = "每页记录数",required = true) @PathVariable("pageSize") Long pageSize){
        Page<Emp> page = new Page<>(pageNum, pageSize);
        empService.page(page);
        return Response.ok(page);
    }
    @Operation(summary = "新增员工")
    @PostMapping
    public Response save(@RequestBody Emp emp){
        empService.save(emp);
        return Response.ok(null);
    }

    @Operation(summary = "更新员工")
    @PutMapping
    public Response update(@RequestBody Emp emp){
        empService.updateById(emp);
        return Response.ok(null);
    }

    @Operation(summary = "删除员工")
    @DeleteMapping("{id}")
    public Response delete(@Parameter(description = "员工id") @PathVariable("id") Long id){
        boolean b = empService.removeById(id);
        if(b){
            //删除成功
            return Response.ok(null);
        }
        //删除失败
        return Response.fail(-2,"id不存在，删除失败");
    }
}
